package jmc.core.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import jmc.core.service.XMLGeneratorManager;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;




public class XMLGeneratorManagerImpl implements XMLGeneratorManager{
	
	public XMLGeneratorManagerImpl() throws IOException{
		
	}
	
	public String generateXML(String root, Hashtable<String,String> childs) throws IOException{
		
		Node n = null;
		
		Document xmlDoc= new DocumentImpl();
		Element rootNode = xmlDoc.createElement(root);
		Enumeration keys = childs.keys();
		while (keys.hasMoreElements()) {
			String element = (String) keys.nextElement();
			String nombre = element;
			String valor = (String) childs.get(element);
			Element nom = xmlDoc.createElementNS(null, nombre);
			n = xmlDoc.createTextNode(valor);
			nom.appendChild(n);
			rootNode.appendChild(nom);
		}
		
		xmlDoc.appendChild(rootNode);
		OutputStream os=new ByteArrayOutputStream();
		OutputFormat of = new OutputFormat("XML","ISO-8859-1",true);
		
		XMLSerializer serializer = new XMLSerializer(os,of);
		serializer.asDOMSerializer();
		serializer.serialize( xmlDoc.getDocumentElement() );
		
		os.close();
		
		return os.toString();
	}

	public String generateXML(String root, String childNames,
			List<String> childs) throws IOException {
		
		Node n = null;
		
		Document xmlDoc= new DocumentImpl();
		Element rootNode = xmlDoc.createElement(root);

		String nombre = childNames;
		Iterator<String> it = childs.iterator();
		while (it.hasNext()) {
			String valor = it.next();
			Element nom = xmlDoc.createElementNS(null, nombre);
			n = xmlDoc.createTextNode(valor);
			nom.appendChild(n);
			rootNode.appendChild(nom);
		}
		
		xmlDoc.appendChild(rootNode);
		OutputStream os=new ByteArrayOutputStream();
		OutputFormat of = new OutputFormat("XML","ISO-8859-1",true);
		
		XMLSerializer serializer = new XMLSerializer(os,of);
		serializer.asDOMSerializer();
		serializer.serialize( xmlDoc.getDocumentElement() );
		
		os.close();
		
		return os.toString();
	}

	public String generateXML(String root, String ChildNames,
			Hashtable<String, String> childs) throws IOException {
		Node n = null;
		
		Document xmlDoc= new DocumentImpl();
		Element rootNode = xmlDoc.createElement(root);
		Enumeration keys = childs.keys();
		while (keys.hasMoreElements()) {
			String element = (String) keys.nextElement();
			String nombre = ChildNames;
			String valor = (String) childs.get(element);
			Element nom = xmlDoc.createElementNS(null, nombre);
			n = xmlDoc.createTextNode(valor);
			nom.appendChild(n);
			nom.setAttribute("id", element);
			rootNode.appendChild(nom);
		}
		
		xmlDoc.appendChild(rootNode);
		OutputStream os=new ByteArrayOutputStream();
		OutputFormat of = new OutputFormat("XML","ISO-8859-1",true);
		
		XMLSerializer serializer = new XMLSerializer(os,of);
		serializer.asDOMSerializer();
		serializer.serialize( xmlDoc.getDocumentElement() );
		
		os.close();
		
		return os.toString();
	}

}
